package com.meichis.yndms.ui.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.view.View;
import android.view.WindowManager;
import android.widget.DatePicker;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.meichis.mcsappframework.encrypt.AESProvider;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.TerminalFeedbackAdapter;
import com.meichis.yndms.databinding.ActivityTerminalFeedbackListBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.TerminalFeedbackVm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * @author : create by  szh
 * @date : 2022/11/10 10:25
 * @desc :终端反馈列表
 */

public class TerminalFeedbackListActivity extends BaseActivity {

    ActivityTerminalFeedbackListBinding binding;
    private TerminalFeedbackVm vm;
    private TerminalFeedbackAdapter adapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_terminal_feedback_list);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(TerminalFeedbackVm.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("终端反馈");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        adapter = new TerminalFeedbackAdapter(this, R.layout.item_terminal_feedback, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);
        binding.tvAll.setSelected(true);

        addObserver();

    }

    @Override
    protected void onResume() {
        super.onResume();
        vm.GetMyFeedbackList();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
        vm.getFeedBackListData().observe(this, feedBacks -> {
            adapter.setList(feedBacks);
        });
    }

    public class ClickEvent {

        public void selectBeginTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getBeginTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void selectEndTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getEndTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void changeState(int state) {
            ArrayList<View> views = new ArrayList<>();
            views.add(binding.tvAll);
            views.add(binding.tvUnReplay);
            views.add(binding.tvReplay);
            views.add(binding.tvEnd);
            setNotSelected(views);
            switch (state) {
                case 0:
                    binding.tvAll.setSelected(true);
                    break;
                case 2:
                    binding.tvUnReplay.setSelected(true);
                    break;
                case 3:
                    binding.tvReplay.setSelected(true);
                    break;
                case 5:
                    binding.tvEnd.setSelected(true);
                    break;
            }
            vm.getState().setValue(state);
            vm.GetMyFeedbackList();
        }

        public void setNotSelected(List<View> views) {
            for (View view : views) {
                view.setSelected(false);
            }
        }

        public void searchFeedBacks() {
            vm.GetMyFeedbackList();
        }

    }
}
